
package com.epam.android.training.day6;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class PhoneContactsActivity extends ListActivity {

    private static String[] PROJECTION = new String[] {
            People._ID, People.NAME, People.NUMBER
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Cursor cur = managedQuery(People.CONTENT_URI, PROJECTION, null, null, null);

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                R.layout.list_item,
                cur,
                new String[] {
                    People.NAME, People.NUMBER
                },
                new int[] {
                    R.id.name_text, R.id.phone_text
                });

        setListAdapter(adapter);
        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Intent intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:" + ((TextView) view.findViewById(R.id.phone_text)).getText()));
                startActivity(intent);
            }
        });
    }
}
